package com.yyge.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyge.common.util.WaimaiThreadLocal;
import com.yyge.common.vo.req.PageReq;
import com.yyge.common.vo.resp.PageResp;
import com.yyge.controller.admin.mapstruct.ProductConverter;
import com.yyge.controller.admin.product.dto.DishFlavorItem;
import com.yyge.controller.admin.product.dto.DishFlavorProperty;
import com.yyge.controller.admin.product.req.ProductAddReq;
import com.yyge.controller.admin.product.req.ProductReq;
import com.yyge.controller.admin.product.resp.ProductResp;
import com.yyge.entity.DishFlavor;
import com.yyge.entity.Product;
import com.yyge.mapper.DishFlavorMapper;
import com.yyge.service.ProductService;
import com.yyge.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* @author Administrator
* @description 针对表【product(商品信息)】的数据库操作Service实现
* @createDate 2023-07-02 20:36:22
*/
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
    implements ProductService{

    /**
     * 商品口味
     */
    @Autowired
    private ProductMapper productMapper;


    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Override
    public PageResp search(ProductReq req) {
        List<ProductResp> arr=productMapper.search(req);
        //需要再一次组装的
        for(ProductResp p: arr){
            LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(DishFlavor::getDishId, p.getId());
            queryWrapper.eq(DishFlavor::getIsDeleted,"0");
            List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(queryWrapper);

            //组装成自己定义的口味数据给前端
            List<DishFlavorProperty> propertyList=new ArrayList<>();

            dishFlavorList.stream().forEach(s-> {
                DishFlavorProperty property=new DishFlavorProperty();
                property.setName(s.getName());

                //json反序列化
                List<DishFlavorItem> dishFlavorItems = JSONUtil.toList(s.getValue(),DishFlavorItem.class);
                property.setValue(dishFlavorItems);
                propertyList.add(property);
            });
            p.setDishFlavors(propertyList);
        }
        PageResp pageResp=new PageResp<>();
        pageResp.setRows(arr);
        pageResp.setPageNum(req.getPage());
        pageResp.setTotal(req.getTotal());
        pageResp.setPageSize(req.getSize());

       return pageResp;
    }


    @Override
    public ProductResp searchById(ProductReq req,String id) {
        List<ProductResp> arr=productMapper.searchById(req,id);

        //需要再一次组装的
        for(ProductResp p: arr){
            LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(DishFlavor::getDishId, p.getId());
            queryWrapper.eq(DishFlavor::getIsDeleted,"0");
            List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(queryWrapper);

            //组装成自己定义的口味数据给前端
            List<DishFlavorProperty> propertyList=new ArrayList<>();

            dishFlavorList.stream().forEach(s-> {
                DishFlavorProperty property=new DishFlavorProperty();
                property.setName( s.getName());
                //json反序列化
                List<DishFlavorItem> dishFlavorItems = JSONUtil.toList(s.getValue(), DishFlavorItem.class);
                property.setValue(dishFlavorItems);
                propertyList.add(property);
            });
            p.setDishFlavors(propertyList);
        }

       return arr.get(0);
    }

    @Override
    public Product addProduct(ProductAddReq req) {
        Product product = ProductConverter.MAPPER.convert(req);
        QueryWrapper wrapper=new QueryWrapper<>(product);
        List list1 = productMapper.selectList(wrapper);

        if(list1.size()!=0){return null;}
        System.out.println("我要准备保存了哦");

        product.setCategoryId(req.getCategoryId());
        product.setMerchantId(WaimaiThreadLocal.getCurrent().getId());

        int insert = productMapper.insert(product);
        if(insert>0){return product;}else {return null;}
    }
}




